/*
 * Created on :Jun 16, 2012 Author :songlin.li
 */
package com.dmagic.cms.common.util.reflection;

import java.sql.Timestamp;
import java.util.Date;

import org.apache.commons.beanutils.Converter;

import com.dmagic.cms.common.util.DateUtil;

public class DateConvert implements Converter {

    public Object convert(Class arg0, Object date) {

        if (date == null) {
            return null;
        } else if (date instanceof java.sql.Date) {
            return DateUtil.convertSQLDateToUtilDate((java.sql.Date) date);
        } else if (date instanceof Timestamp) {
            return DateUtil.convertSQLTimestampToUtilDate((Timestamp) date);
        } else if (date instanceof String) {
            Date d = null;
            try {
                d = DateUtil.stringToDate((String) date);
            } catch (Exception e) {
                d = DateUtil.stringToDate((String) date, DateUtil.FORMAT_6);
                if (d == null) {
                    d = DateUtil.stringToDate((String) date, DateUtil.FORMAT_7);
                }
            }
            return d;
        } else {
            return null;
        }
    }
}
